<?php 

Class PersonAction extends CommonAction{

    public function __construct()
    {
        parent::__construct();
        if (!session("?" . C("SESSION_USER_ID")) || !session("?" . C("SESSION_USER_NAME"))) {
            // $this->redirect('/Login/index',null,3,"未登陆！正在跳转登陆页面！");
            $this->error('未登陆！正在跳转登陆页面！', U(GROUP_NAME . '/Login/index'));
        }
    }
    /**
    * display user profile page
    * 
    * @return [type] [description]
    */
    public function index(){
        $this->profile = M("User")->where('u_id = ' . session(C("SESSION_USER_ID")))->find();
        $this->user_profile = "active";
        $this->right = "user_info";
        
        $this->display('index');
        // p($this->profile, "profile");
    }
 
    /**
    * modify user profile
    * @return [type] [description]
    */
    public function updateProfileHandle()
    {
        if(!IS_POST) halt("页面不存在！");
        if (M("User")->save($_POST)) {
            $this->success('更新个人信息成功！');
        } else {
            $this->error('更新个人信息失败！');
        }
    }

    public function changePassword()
    {
        $this->change_password = "active";
        $this->right = "changePassword";
        
        $this->display('index');
    }

    public function changePasswordHandle()
    {
        p($_POST);
        if(!IS_POST) halt("页面不存在！");
        $oldPassword = I("oldPassword");
        $newPassword = I("newPassword");

        $user = M("User")->where('u_id = '. session(C("SESSION_USER_ID")))->find();

        if (encode($oldPassword) == $user['password']) {
            $data['u_id'] = session(C("SESSION_USER_ID"));
            $data['password'] = encode($newPassword);
            if (M("User")->save($data)) {
                session(C("SESSION_USER_ID"), null);
                session(C("SESSION_USER_NAME"), null);
                $this->success('修改密码成功！请重新登录！', U(GROUP_NAME . '/Login'));

            } else {
                $this->error('修改密码失败！');
            }
            
        } else {
            $this->error('原密码错误！请重新填写！');
        }
    }

    /**
     * display add doc page
     */
    public function addDoc()
    {
        $this->add_doc = "active";
        $this->right = "add_doc";
        
        $this->display('index');
    }

    /**
    * handle add doc operation 
    */
    public function addDocHandle()
    {
        // p($_POST);
        if(!IS_POST) halt("页面不存在！");

        $data['docname'] = I("docname");
        $data['status'] = 0;
        $data['abstract'] = I("abstract");
        $data['u_id'] = session(C("SESSION_USER_ID"));
        $data['createtime'] = date("Y-m-d H:i:s");
        
        if (M("Document")->add($data)) {
            $this->success("添加文献成功！", U(GROUP_NAME . '/Person/doc'));
        } else {
            $this->error("添加文献失败！");
        } 
    }

    /**
    * display doc list page
    * @return [type] [description]
    */
    public function doc()
    {
        $Data = M('Document'); // 实例化Data数据对象
        import('ORG.Util.Page');// 导入分页类
        $count      = $Data->where("u_id = " . session(C("SESSION_USER_ID")))->count();// 查询满足要求的总记录数
        $Page       = new Page($count, 8);// 实例化分页类 传入总记录数
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $this->page = $Page->data();

        $this->doc = $Data->where('u_id = ' . session(C("SESSION_USER_ID")))->page($nowPage, 8)->select();
        $this->right = "doc_management";
        $this->doc_manage = "active";
        $this->display('index');

        // p($this->doc, "doc");
        // p($this->page, "page");
    }

    public function doc_edit()
    {
        $d_id = I("d_id");

        if(!empty($d_id)) {
            $this->doc = M("Document")->where('d_id = ' . $d_id)->find();
        } else {
            halt("页面错误！");
        }

        $this->display();
        // p($this->doc, "doc");
    }

    /**
    * handle update doc operation
    * @return [type] [description]
    */
    public function updateDocHandle()
    {
        if (M("Document")->save($_POST)) {
            $this->success("更新文献信息成功！", U(GROUP_NAME . '/Person/doc_edit', array("d_id" => I("d_id"))));
        } else {
            $this->error("更新文献信息失败！");
        } 
    }

    /**
    * handle del doc operation
    * @return [type] [description]
    */
    public function deleteDocHandle()
    {
        $d_id = I("d_id");

        if(!empty($d_id)) {
            if (D("Document")->delDocByDid($d_id)) {
                // $this->display('index');
                $this->success('删除文献成功！');
            } else {
                // $this->display('index');
                $this->success('删除文献失败！请查看该文献是否已建立圈子！');
            }
            
        } else {
            halt("页面错误！");
        }
    }

    /**
    * display item list page
    * @return [type] [description]
    */
    public function item()
    {
        $Data = D('Item'); // 实例化Data数据对象
        import('ORG.Util.Page');// 导入分页类
        $count      = $Data->where("u_id = 1")->count();// 查询满足要求的总记录数
        $Page       = new Page($count, 8);// 实例化分页类 传入总记录数
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $this->page = $Page->data();

        $this->item = $Data->where('u_id = 1')
                           ->page($nowPage, 8)
                           ->relation("Document")
                           ->select();

        $this->item_manage = "active";
        $this->right = "item_management";
        $this->display('index');
        // p($this->item, "item");
        // p($this->page, "page");
    }

    /**
     * handle add item operation
     * @return [type] [description]
     */
    public function addItemHandle()
    {
        p($_POST);
    }

    /**
    * handle del item operation
    * @return [type] [description]
    */
    public function deleteItemHandle()
    {
        // p($_GET);

        $i_id = I("i_id");

        if(!empty($i_id)) {
            if (D("Item")->delItemByIid($i_id)) {
                // $this->display('index');
                $this->success('删除词条成功！');
            } else {
                // $this->display('index');
                $this->success('删除词条失败！');
            }
            
        } else {
            halt("页面错误！");
        }

    }

    /**
     * handle update item operation
     * @return [type] [description]
     */
    public function updateItemHandle()
    {
        p($_POST);
    }


    public function applyCircle()
    {
        $this->doc = M("Document")->where('u_id = ' . session(C("SESSION_USER_ID")))
                                  ->field("d_id, docname")
                                  ->select();
        $this->apply_circle = "active";
        $this->right = "applyCircle";
        $this->display('index');
        // p($this->doc, "doc");
    }

    public function applyCircleHandle()
    {
        if(!IS_POST) halt("页面不存在！");
        $d_id = I(" d_id");
        $content = I("content");
        $content = "创建圈子申请！文献ID：$d_id" . $content;

        $model = new Model();
        $max_id = $model->query("select max(a_id) as max_id from dict_application limit 0,1 ");
        $next_id = $max_id[0]['max_id'] + 1;
        $path = "./Public/Home/file/applyCircle" . $next_id . ".txt";
        writeToFile($path, $content);

        $data['d_id'] = $d_id;
        $data['u_id'] = session(C("SESSION_USER_ID"));
        $data['content'] = $path;
        $data['status'] = 0;

        if (M("Application")->add($data)) {
            $this->success('提交申请成功！请耐心等待审核！');
        } else {
            $this->success('提交申请失败！');
        }
        
        // // p($next_id[0]['max_id'] + 1);
        // p($next_id);
        // p($content);
    }

    public function deleteCircleHandle()
    {
        p($_POST);

        // if(!IS_POST) halt("页面不存在！");
        // $d_id = I(" d_id");
        // $content = I("content");
        // $content = "创建圈子申请！文献ID：$d_id" . $content;

        // $model = new Model();
        // $max_id = $model->query("select max(a_id) as max_id from dict_application limit 0,1 ");
        // $next_id = $max_id[0]['max_id'] + 1;
        // $path = "./Public/Home/file/applyCircle" . $next_id . ".txt";
        // writeToFile($path, $content);

        // $data['d_id'] = $d_id;
        // $data['u_id'] = session(C("SESSION_USER_ID"));
        // $data['content'] = $path;
        // $data['status'] = 0;

        // if (M("Application")->add($data)) {
        //     $this->success('提交申请成功！请耐心等待审核！');
        // } else {
        //     $this->success('提交申请失败！');
        // }
    }

    public function circle()
    {
        // $Data = M('CircleUser'); // 实例化Data数据对象
        // import('ORG.Util.Page');// 导入分页类
        // $count      = $Data->where("u_id = 1")->count();// 查询满足要求的总记录数
        // $Page       = new Page($count, 8);// 实例化分页类 传入总记录数
        // $nowPage = isset($_GET['p'])?$_GET['p']:1;
        // $this->page = $Page->data();

        // $this->doc = $Data->where('u_id = 1')->page($nowPage, 8)->select();
        $model = new Model();
        $this->circle = $model->query("SELECT dcu.c_id, dcu.u_id,  dcu.degree, dc.hot, dd.docname, dc.status, dc.createtime
                                       from dict_circle_user dcu, dict_circle dc, dict_document dd
                                       WHERE dcu.c_id = dc.c_id and dc.c_id = dd.d_id 
                                       and dcu.u_id = " . session(C("SESSION_USER_ID")));
        $this->circle_manage = "active";
        $this->right = "circle_management";
        $this->display('index');

        // p($this->circle, "circle");
        // p($this->page, "page");
    }

  /**
   * display message page
   * @return [type] [description]
   */
    public function message()
    {
        $this->read = D("Message")->getAllReadMessageByUid(1);
        $this->unread = D("Message")->getAllUnreadMessageByUid(1);

        $this->user_message = "active";
        $this->right = "message";
        $this->display('index');
        // p($this->read, "read");
        // p($this->unread, "unread");
    }

    /**
     * handle message click
     * @return [type] [description]
     */
    public function messageHandle()
    {
        p($_GET);
    }

    public function addExampleHandle()
    {
        // p($_POST);
        $data['i_id'] = I("i_id");
        $data['sentence'] = I("sentence");
        $data['definition'] = I("definition");
        $data['createtime'] = date("Y-m-d H:i:s");

        if (M("Example")->add($data)) {
            $this->success('添加例子成功！');
        } else {
            $this->success('添加例子失败！');
        }
    }

    public function delExampleHandle()
    {
        p($_POST);
    }

    public function updateExampleHandle()
    {
        p($_POST);
    }
    public function work_platform()
    {
        $d_id = $_GET['d_id'];
        $s = I("s");

        $Data = D('Item'); // 实例化Data数据对象
        import('ORG.Util.Page');// 导入分页类

        if (!empty($s)) {
            if(!IS_POST) halt("页面错误！");
            $count      = $Data->where("u_id = 1 and itemname like '%$s%'")->count();// 查询满足要求的总记录数
            $Page       = new Page($count, 20);// 实例化分页类 传入总记录数
            $nowPage = isset($_GET['p'])?$_GET['p']:1;
            $this->page = $Page->data();

            $this->item = $Data->where("u_id = 1  and itemname like '%$s%'")
                               ->page($nowPage, 20)
                               ->relation(true)
                               ->select();

            $this->doc = M("Document")->field("d_id, docname")
                                      ->where('d_id = ' . $d_id)
                                      ->find();

            $this->s = $s;
        } else {

            $count      = $Data->where("u_id = 1")->count();// 查询满足要求的总记录数
            $Page       = new Page($count, 20);// 实例化分页类 传入总记录数
            $nowPage = isset($_GET['p'])?$_GET['p']:1;
            $this->page = $Page->data();

            $this->item = $Data->where('u_id = 1')
                               ->page($nowPage, 20)
                               ->relation(true)
                               ->select();

            $this->doc = M("Document")->field("d_id, docname")->where('d_id = ' . $d_id)->find();
            
        }

        $this->display();
        // p($this->page, "page");
        // p($this->item, "item");
        // p($this->doc, "doc");
        // p($d_id, "d_id");
    }

    /**
         * 
         * @return [type] [description]
         */
        public function commentHandle()
        {
            if(!IS_POST) halt("页面不存在！");
            $data['u_id'] = I("u_id");
            $data['t_id'] = I("t_id");
            $data['reply'] = I("reply");
            $data['content'] = I("content");
            $data['createtime'] = date("Y-m-d H:i:s");

            $message['type'] = 1;
            $message['content'] = "";
            $message['status'] = 0;
            // $message['url']
            $message['u_id'] = 1;
            $message['createtime'] = date("Y-m-d H:i:s");

            if (M("Comment")->add($data) && M("Message")->add($message)) {
                $this->success("评论成功！");
            } else {
                $this->error('评论失败');
            }
            
        }
}

 ?>
